layui.define(['jquery', 'layer', 'suggest', 'configureCom', 'configure', 'sessionFun', 'bluebird'], function (exports) {
    var $ = layui.jquery;
    var layer = layui.layer;
    var suggest = layui.suggest;
    var configureCom = layui.configureCom;
    var configure = layui.configure;
    var sessionFun = layui.sessionFun;
    var pdfTimer;
    var pdf = {
        // pdf初始化
        init: function (id, url, boxId) {
            var html = '<object classid="clsid:7017318C-BC50-4DAF-9E4A-10AC8364C315" codebase="iStylePDF.cab#version=2,0,5,1103" id="' + id + '" height="100%"  width="100%" ></object>';
            $(boxId).html(html);
            pdfTimer = setInterval(function () {
                try{
                    clearInterval(pdfTimer);
                    pdfTimer=null;
                    pdf.openWebDoc(url, id, boxId);
                }catch(err){}
            }, 700);
        },

        updateCircle: function (id) {
            //初始化pdf时，加载圈阅意见坐标

            suggest.getList().then(function (res) {
                $(res.result.content).each(function (index, item) {
                    if (item.coordinate) {
                        var e = $('#' + id)[0].Documents.ActiveDocument.ImportAnnotation(item.coordinate);
                        if (e) {
                            e.Locked = true;
                        }
                    }
                });
            });
        },
        //清空pdf圈阅信息
        removeCircle: function (randomid) {
            if (randomid) {
                var iStylePDF = $('#iStylePDF')[0];
                var pages = iStylePDF.Documents.ActiveDocument.Pages;
                for (var pageIndex = 0; pageIndex < pages.Count; pageIndex++) {
                    var annots = pages.Item(pageIndex).Annots;
                    for (var annotIndex = 0; annotIndex < annots.Count; annotIndex++) {
                        var annot = annots.Item(annotIndex);
                        if (annot.UniqueName.replace(/-/g, '') == randomid) {
                            annot.Delete();
                            return;
                        }
                    }
                }
            }
        },
        //意见添加
        suggestAdd: function (Annot, wParam, lParam) {
            sessionFun.remove(configure.SESSION_SUGGEST_DATA);
            var title = configure.LAYER_TITLE_ADD_SUGGEST;
            var url = configure.EXPERT_ADD_SUGGEST_HTML + '?type=add';
            configureCom.klmyPop([url, title, 1], false, false, function (layero, index) {
                window.saveForm = function (data) {
                    data.coordinate = Annot.Export();//记录pdf圈阅坐标
                    data.randomId = Annot.UniqueName.replace(/-/g, '');//记录pdf圈阅信息
                    suggest.add(data).then(function (res) {
                        top.layer.alert(configure.MSG_ADD);
                    });
                    top.layer.close(index);
                };
            });
        },
        //意见查看
        suggestRead: function (EventName, Addin, wParam, lParam) {
            sessionFun.remove(configure.SESSION_SUGGEST_DATA);
            var randomId = Addin.Annotation.UniqueName.replace(/-/g, '');//记录pdf圈阅坐标
            suggest.getList(randomId).then(function (res) {//获取当前pdf圈阅的数据
                sessionFun.set(configure.SESSION_SUGGEST_DATA, res.result.content[0]);//将当前pdf圈阅数据保存如session
                var title = '意见查看';
                var url = configure.EXPERT_SUGGEST_DETAIL_HTML;
                configureCom.klmyPop([url, title]);
            });
        },
        //意见修改
        suggestEdit: function (EventName, Addin, wParam, lParam) {
            sessionFun.remove(configure.SESSION_SUGGEST_DATA);
            var randomId = Addin.Annotation.UniqueName.replace(/-/g, '');//记录pdf圈阅坐标
            suggest.getList(randomId).then(function (res) {//获取当前pdf圈阅的数据
                sessionFun.set(configure.SESSION_SUGGEST_DATA, res.result.content[0]);//将当前pdf圈阅数据保存如session
                var title = configure.LAYER_TITLE_CHANGE_SUGGEST;
                var url = configure.EXPERT_ADD_SUGGEST_HTML + '?type=edit';
                configureCom.klmyPop([url, title, 1], false, false, function (layero, index) {
                    window.saveForm = function (data) {
                        data.randomId = randomId;
                        suggest.change(data).then(function (res) {
                            top.layer.alert(configure.MSG_ADD);
                        });
                        top.layer.close(index);
                    };
                });
            });
        },
        // 打开pdf
        openWebDoc: function (url, id, boxId) {
            clearTimeout(pdfTimer);
            try {
                $('#' + id)[0].Documents.OpenFromURL(url);
                var pdfTime = setTimeout(function () {
                    clearTimeout(pdfTime);
                    //初始化pdf时，加载圈阅意见坐标
                    pdf.updateCircle(id);
                }, 1000);
            } catch (err) {
                layer.alert('请安装pdf控件');
            }
        }
    };
    exports('pdf', pdf);
});